package com.admin.common.util;


import com.admin.mapper.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



@Component
public class CommonUtil {

    @Autowired
    private RegionMapper regionMapper;

/*
    *//**
     * 采购项目 - 合同编号
     *
     * @param projectType
     * @return
     *//*
    public String getBuyContractSn(Integer projectType) {
        //合同编号-前缀
        String prefix = ContractSnTypeEnum.getText(projectType);
        //年月
        String yyyyMM = DateUtils.format(new Date(), "yyyyMM");
        //序号，上一个序号编码
        String lastContractSn = contractMapper.getLastContractSnByType(projectType);
        int numValue  = 1;
        if (!StringUtils.isEmpty(lastContractSn)) {
            String[] lastSnArray = lastContractSn.split("-");
            if (null != lastSnArray && lastSnArray.length > 1) {
                numValue = Integer.valueOf(lastSnArray[1]) + 1;
            }
        }
        String lastPrefix = new DecimalFormat("000").format(numValue);
        //填充4位
        if (numValue >= 1000) {
            lastPrefix = new DecimalFormat("0000").format(numValue);
        }
        StringBuffer snBuffer = new StringBuffer();
        snBuffer.append(prefix).append(yyyyMM).append("-").append(lastPrefix);
        String sn =  snBuffer.toString();
        //校验编码是否被占用
        if (contractMapper.getByContractSnAndProjectType(sn, projectType) == null) {
            return sn;
        }
        return getBuyContractSn(projectType);
    }


    *//**
     * 以杭州西湖区为例：LTXSHZXH202203-001
     *
     * 以杭州西湖区为例：LTXSHZBJ202203-001
     *
     * @return
     *//*
    public String getSaleContractSn(Integer customerId,Integer projectType) {
        Customer customer =  customerMapper.selectByPrimaryKey(customerId);
        if (Objects.isNull(customer) || customer.getCityId() == null || customer.getAreaId() == null) {
            throw new BizException(-1, "生成合同编码异常,销售客户不存在或未设置市区县地址!");
        }
        if (StringUtils.isEmpty(customer.getCompanyLevel())) {
            throw new BizException(-1, "生成合同编码异常,客户对应的身份不存在或已删除!");
        }

        Region regionCity = regionMapper.selectByPrimaryKey(customer.getCityId());

        Region regionArea = regionMapper.selectByPrimaryKey(customer.getAreaId());

        //是否是本级
        Boolean bool = false;
        if (null != customer.getCompanyLevel() && customer.getCompanyLevel().equals(CompanyLevelEnum.BJ.getKey())) {
            bool = true;
        }
        //前缀
        String prefix = ContractSnTypeEnum.getText(projectType);
        //如果销售和维保需要加 年月
        String yyyyMM  = DateUtils.format(new Date(), "yyyyMM");
        //中间编码
        String moddleStr;
        //是否是本级
        if (bool) {
            moddleStr = regionCity.getJianpin() + CompanyLevelEnum.BJ.getKey();
        } else {
            moddleStr = regionCity.getJianpin() + regionArea.getJianpin();
        }
        String lastContractSn = contractMapper.getLastContractSnByType(projectType);
        int numValue  = 1;
        if (!StringUtils.isEmpty(lastContractSn)) {
            String[] lastSnArray = lastContractSn.split("-");
            if (null != lastSnArray && lastSnArray.length > 1) {
                numValue = Integer.valueOf(lastSnArray[1]) + 1;
            }
        }
        String lastPrefix = new DecimalFormat("000").format(numValue);
        //填充4位
        if (numValue >= 1000) {
            lastPrefix = new DecimalFormat("0000").format(numValue);
        }
        StringBuffer snBuffer = new StringBuffer();
        snBuffer.append(prefix).append(moddleStr).append(yyyyMM).append("-").append(lastPrefix);
        String sn =  snBuffer.toString();
        if (contractMapper.getByContractSnAndProjectType(sn, projectType) == null) {
            return sn;
        }
        return getSaleContractSn(projectType, customerId);
    }


    *//**
     * 采购单编码
     * @return
     *//*
    //订单号  生成规则：年月日时分+6位随机数
    public String getOrderSn() {
        String sn = new SimpleDateFormat("yyyyMMddHHmm").format(new Date(System.currentTimeMillis())) + RandomUtils.nextInt(100000, 999999);
        if (orderMapper.getByOrderSn(sn) == null) {
            return sn;
        }
        return getOrderSn();
    }

    *//**
     * 客户编码
     * LTKH+四位自增式编码
     * @return
     *//*
    public String getCustomerSn() {
        String prefix = "LTKH";
        //后5位自增编码
        String lastPrefix = "0001";
        String lastContractSn = customerMapper.getLastCustomerSn();
        if (!StringUtils.isEmpty(lastContractSn)) {
            //截取后自增编码
            String tmpLastContractSn = lastContractSn.substring(4, lastContractSn.length());

            int numValue = Integer.valueOf(tmpLastContractSn) + 1;
            //填充4位
            lastPrefix = new DecimalFormat("0000").format(numValue);
        }
        String sn = prefix + lastPrefix;
        if (customerMapper.getCustomerSn(sn) == null) {
            return sn;
        }
        return getCustomerSn();
    }


    //出库单号  生成规则：CK+年月+10位随机数
    public String getCKOutboundNo() {
        String prefix = "CK";
        String outboundNo = prefix + DateUtils.getCurrentMonth() + RandomUtils.nextLong(1000000000L, 9999999999L);
        if (outboundBillsMapper.getByBillSn(outboundNo) == null) {
            return outboundNo;
        }
        return getCKOutboundNo();
    }

    //入库单号  生成规则：RK+10位随机数
    public String getInBoundNo() {
        String inboundNo = "RK" + RandomUtils.nextLong(1000000000L, 9999999999L);
        if (outboundBillsMapper.getByBillSn(inboundNo) == null) {
            return inboundNo;
        }
        return getInBoundNo();
    }*/

}
